package com.potenza.training.interview.controller;

import com.aliyun.oss.OSS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.potenza.training.interview.config.Result;
import com.potenza.training.interview.dao.InterviewRecordDao;
import com.potenza.training.interview.entity.InterviewRecordEntiy;
import com.potenza.training.interview.feign.SysServiceFegin;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("interview/Oss")
public class OssContor {
    @Autowired
    private OSS oss;
    @Value("${oss.hostName}")
    String hostName;
    @Value("${oss.bucket}")
    String bucket;
    @Autowired
    SysServiceFegin sysServiceFegin;

    @Autowired
    InterviewRecordDao interviewRecordDao;


    @PostMapping("imagefile/{id}")
    public Result imagefile(@RequestParam("audio") MultipartFile file, @PathVariable Integer id) throws IOException {
        System.out.println(id+"-------------------------------------------------------------------");
        String filename1 = file.getOriginalFilename();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        String ss=format+filename1;
        oss.putObject(bucket,ss,file.getInputStream());
        String message="https://"+bucket+"."+hostName+"/"+ss;
        System.out.println(message+"-----------");


        R info = sysServiceFegin.info();
        Map user = (Map) info.get("user");
        InterviewRecordEntiy interviewRecordEntiy = new InterviewRecordEntiy();
         interviewRecordEntiy.setSoundRecording(message);
         interviewRecordEntiy.setInvitationId(id);
         interviewRecordEntiy.setUid(Long.parseLong(user.get("userId").toString()));
        interviewRecordDao.insert(interviewRecordEntiy);
        return new Result(true,"1",message);

    }

    @GetMapping("findbyid/{id}")
    public List<InterviewRecordEntiy> findbyid(@PathVariable Integer id){
        QueryWrapper<InterviewRecordEntiy> interviewRecordEntiyQueryWrapper = new QueryWrapper<>();
       interviewRecordEntiyQueryWrapper.lambda().eq(InterviewRecordEntiy::getInvitationId,id);

        List<InterviewRecordEntiy> interviewRecordEntiys = interviewRecordDao.selectList(interviewRecordEntiyQueryWrapper);
        return  interviewRecordEntiys;

    }
}